﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using System.Xml;
using Microsoft.Xaml.Behaviors;
using Serilog;
using Serilog.Core;
using SimpleTool.SerialTool.SerialControl;
using SimpleTool.Server;
using static System.Collections.Specialized.BitVector32;

namespace SimpleTool.Behavior
{
    public class ListBoxBehavior : Behavior<ListBox>
    {

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.PreviewMouseDown += OnPreviewMouseDown;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.PreviewMouseDown -= OnPreviewMouseDown;
        }

        private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton != MouseButton.Left)
            {
                e.Handled = true;
            }
        }
    }

    public class TextBoxBehavior : Behavior<TextBox>
    {
        public static readonly ILogger Logger = Log.ForContext<TextBoxBehavior>();

        public static DependencyProperty MyReadOnlyProperty = DependencyProperty.Register(
            nameof(MyReadOnly),
            typeof(bool),
            typeof(TextBoxBehavior),
            new PropertyMetadata(true, IsReadOnlyChange)
            );

        public bool MyReadOnly
        {
            set
            {
                SetValue(MyReadOnlyProperty, value);
            }
            get
            {
                return (bool)GetValue(MyReadOnlyProperty);
            }
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.IsReadOnly = true;
            AssociatedObject.GotFocus += SelectedChange;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
        }

        public static void IsReadOnlyChange(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            TextBoxBehavior self = (TextBoxBehavior)obj;
            if (e.NewValue is bool state) 
            {
                Application.Current.Dispatcher.Invoke(async () =>
                {
                    if (self.AssociatedObject != null)
                    {
                        Keyboard.ClearFocus(); //**//

                        await Task.Delay(30);

                        self.AssociatedObject.IsReadOnly = state;
                        if (!state)
                        {
                            self.AssociatedObject.Focus();
                            InputMethod.SetIsInputMethodEnabled(self.AssociatedObject, true);
                            InputMethod.SetPreferredImeState(self.AssociatedObject, InputMethodState.On);
                        }
                    }
                });
            }
        }

        public static void SelectedChange(object sender, RoutedEventArgs e)
        {

        }
    }
}

/**
var hyperlink = new Hyperlink(new Run("点击访问"));
hyperlink.NavigateUri = new Uri("https://example.com");
hyperlink.RequestNavigate += (s, e) => Process.Start(e.Uri.ToString());
paragraph.Inlines.Add(hyperlink);
**/